package com.easy.work.function.city;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.syslib.util.TextUtil;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.city.adapter.ProvinceAdapter;
import com.easy.work.function.mall.activity.JobFilterActivity;
import com.easy.work.function.mall.bean.JobCommonBean;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.EducationRequest;
import com.easy.work.http.request.JobFilterRequest;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2018/1/7.
 * 省
 */
public class ProvinceAct extends BaseActivity {
    @BindView(R.id.hostitaldqselect_main_list)
    ListView hostitaldqselectMainList;
    @BindView(R.id.hostitaldqselect_sub_list)
    ListView hostitaldqselectSubList;
    ProvinceAdapter proAdapter;
    ProvinceAdapter cityAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, ProvinceAct.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_select_city);
        ButterKnife.bind(this);
        initView();
        getProList("");
    }

    protected void initView() {

        initTitle("选择城市");
        hostitaldqselectMainList = (ListView) findViewById(R.id.hostitaldqselect_main_list);
        hostitaldqselectSubList = (ListView) findViewById(R.id.hostitaldqselect_sub_list);

        hostitaldqselectMainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                proAdapter.setSelectItem(position);
                proAdapter.notifyDataSetChanged();
                getProList(proList.get(position).key);
            }
        });

        hostitaldqselectSubList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JobCommonBean.ItemsEntity city = cityList.get(position);
                AppSession.getInstance().setCityName(city.text);
//                final JobFilterRequest request = new JobFilterRequest();
//                request.city = city.text;
                finish();
                EventBus.getDefault().post(city);

            }
        });
    }

    private void getProList(final String parentKey) {
        getEducationList("es.base.dictionary.AddrArea", parentKey);
    }

    /**
     * 获取学历或者薪资数据
     *
     * @param dicId     字典
     * @param parentKey
     */
    private void getEducationList(String dicId, final String parentKey) {
        final EducationRequest request = new EducationRequest();
        request.dicId = dicId;
        request.parentKey = parentKey;
        request.sliceType = "3";
        request.query = "";
        request.start = 0;
        request.limit = 0;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                handleData(response, parentKey);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
            }
        });
    }

    /**
     * 学历或薪资数据处理
     */
    private void handleData(BaseResponse response, String key) {
        JobCommonBean bean = (JobCommonBean) response;
        if (bean != null && bean.items != null && !bean.items.isEmpty()) {
            List<JobCommonBean.ItemsEntity> list = bean.items;
            if (TextUtil.isNotNull(key)) {

                cityList = list;
                cityAdapter = new ProvinceAdapter(oThis, cityList);
                hostitaldqselectSubList.setAdapter(cityAdapter);
            } else {
                proList = list;
                proAdapter = new ProvinceAdapter(oThis, proList);
                hostitaldqselectMainList.setAdapter(proAdapter);
                getProList(proList.get(0).key);

            }
        }
    }


    private List<JobCommonBean.ItemsEntity> proList = new ArrayList<>();
    private List<JobCommonBean.ItemsEntity> cityList = new ArrayList<>();
}
